module.exports = function ($scope, $resource, $modal, FileUploader, toaster, date2str, str2date) {
  $scope.vm = {
    'date' : '',
    'options' : {
        format: "YYYY-MM-DD",
        locale : 'zh-cn'
    }
  }
  // 区县
  $scope.districtArr = [
    {'label' : '和平区', 'value' : '0'},
    {'label' : '沈河区', 'value' : '1'},
    {'label' : '皇姑区 ', 'value' : '2'},
    {'label' : '铁西区', 'value' : '3'},
    {'label' : '大东区', 'value' : '4'},
    {'label' : '东陵区', 'value' : '5'},
    {'label' : '于洪区', 'value' : '6'},
    {'label' : '苏家屯区', 'value' : '7'},
    {'label' : '沈北新区', 'value' : '8'},
    {'label' : '浑南新区', 'value' : '9'},
    {'label' : '辽中县', 'value' : '10'},
    {'label' : '康平县', 'value' : '11'},
    {'label' : '法库县', 'value' : '12'},
    {'label' : '新民市', 'value' : '13'}
  ]
  $scope.company = {};
  $scope.selection = function(aa){
    $scope.info.user_country = aa.value;
    $scope.info.user_country_name = aa.label;
  }
	init();
	//查询当前机构是否已申报
	function init() {
    // $scope.company = {};
    $scope.info = {};
		$scope.fromData = {
			'register': true
		}
		//无参数执行添加,查看当前机构申报状态
		$resource('/api/ac/ggc/touristFundsUserinfoService/getCompanyInfo', {}, {}).save({}, function(res){
      console.log(res);
      if (res.errcode === 0) {
				$scope.fromData.status = res.data.info;
				if(res.data.info == '0'){ // 直接注册
					// $scope.fromData.msg = '已经申报招徕奖励, 无需再次申报.';
				} else if(res.data.info == '1'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '正在审核, 请耐心等待.';
				} else if(res.data.info == '2'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '审核已通过, 可申报项目.';
				} else if(res.data.info == '3'){
          $scope.fromData.register = false;
					$scope.fromData.msg = '审核已驳回, 请重新申报项目.';
				}
      } else {
				$scope.fromData.register = false;
			}
    });
  }
  
  // 查询注册信息
  $scope.registerInfo = function(){
    $resource('/api/as/sc/office/getCompanyBaseInfo', {}, {}).save({}, function(res){
      if (res.errcode !== 0) {
        toaster.error({ title: "提示", body: res.errmsg});
        return;
      }
      console.log(res);
      $scope.company = res.data;
    });
  }
  $scope.registerInfo();

	//企业申报
	$scope.ok = function() {
    $scope.info.user_company_name = $scope.company.name;
    $scope.info.user_license_img = $scope.company.license_img;
    $scope.info.user_address = $scope.info.user_address;
    $scope.info.user_email = $scope.info.user_email;
    $scope.info.company_start_date = date2str($scope.vm.date._d);
    
		if(!checkInfo($scope.info)) return;
    // $scope.company.status = $scope.fromData.status;
    $resource('/api/ac/ggc/touristFundsUserinfoService/createTouristFundsUserinfo', {}, {}).save($scope.info, function(res){
      if (res.errcode !== 0) {
        toaster.error({ title: "提示", body: res.errmsg});
        return;
      }
      console.log(res);
      toaster.success({ title: "提示", body: res.data.msg});
	    init();
      
    });
		// $resource('/api/ac/atc/companyService/addCompany', {}, {}).save($scope.company, function(res){
        //     if (res.errcode === 0 && res.data.info == '1') {
		// 		$scope.fromData.register = false;
		// 		$scope.fromData.status = '0'; //任意值都可
		// 		$scope.fromData.msg = '申报成功, 请耐心等待审核结果.';
        //     } else {
        //         toaster.error({title: '', body: '申请失败, 请重试'});
        //     }
        // });
	}

	//重新申报
	$scope.reApply = function() {
		$scope.fromData.register = true;
	}


	$scope.uploader = new FileUploader({
      url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	
	//上传图片成
	$scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
      $scope.company.license_img = response.savename;
	}

	//校验企业信息
	function checkInfo(info){
		if(!info.user_name){
			toaster.warning({title: '', body: '请填写负责人名称.'});
			return false;
		}
		if(!info.user_phone){
			toaster.warning({title: '', body: '请填写负责人手机号'});
			return false;
		}
		if(!/^1[3|5|7|8|9]\d{9}$/.test(info.user_phone)){
			toaster.warning({title: '', body: '手机号格式不正确'});
			return false;
		}
		if(!info.user_license){
			toaster.warning({title: '', body: '请填写执照编号.'});
			return false;
		}
		if(!info.user_license_img){
			toaster.warning({title: '', body: '请上传营业执照图片.'});
			return false;
		}
		return true;
	}

};